<?php

	require_once('../process/connect_db.php');
	$db = new bd("admins");
	$query="Select nom_torneig From admins";
	$t=$db->consulta($query);	
	
	$trobat = false;
	for ($i=0; $i < sizeof($t) && !$trobat; $i++) { 
		if($t[$i]['nom_torneig'] == $_GET['torneig']){
			$trobat = true;
		}
	}
	
	
	if(!$trobat)	header("Location: index.php");
	
	$query = "Select nom_db From admins Where nom_torneig = \"". $_GET['torneig']. "\"";
	$db_torneig = $db->consulta($query);	

	$db = new bd($db_torneig[0]['nom_db']);
?>

<html>
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Generador d'horaris</title>
        <link rel="stylesheet" type="text/css" href="../estils/style.css">
	</head>
    <body>

	    	<div id="capçal" class="ombra">
	    	
	    		<h1 id="h1_head"> Generador d'horaris<h1>

			</div>
		

		<div  id="main" class="ombra">
			<div id="menu">
				<ul>
					<li class="nivell1"><a class="nivell1" href="../forms/index.php">Inici</a></li>
					<li class="nivell1"><a class="nivell1" href="#">Participants</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_select_torneig.php">Inscriure's</a></li>
							<li><a href="#">Informació</a></li>
							<li><a href="#">Fotos</a></li>
						</ul>
					</li>
					<li class="nivell1"><a class="nivell1" href="#">Organització</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_admin.php">Administrar torneig</a></li>
							<li><a href="#">Crear torneig</a></li>
						</ul>
					</li>
				</ul>
			</div>	

			<script>
				var descrips = new Array();
				var min_part = new Array();
				var max_part = new Array();

				<?php
					$query = "Select nom, descripcio, min_part, max_part From categories";
					$cats = $db->consulta($query);
					for ($i=0; $i < sizeof($cats); $i++) { 
				?>
					descrips[<?php echo $i?>]="<?php if(isset($cats[$i]['descripcio']))echo $cats[$i]['descripcio'];?>";
					min_part[<?php echo $i?>]="<?php echo $cats[$i]['min_part']?>";
					max_part[<?php echo $i?>]="<?php echo $cats[$i]['max_part']?>";
				<?php	
					}
				?>
				
				function actualitzaInfo(){

					var cat = document.inscrip.cat.selectedIndex;
					document.getElementById('desc').innerHTML = descrips[cat];
					
					var txt = "";
					
					for (var i=0; i < max_part[cat]; i++) {
						if(i < min_part[cat]){
							txt += 'Participant '+ (i+1) +': <input type="text" name="part'+i+' " required="required" autofocus="autofocus"><br><br>';
						} else {
							txt += '(Opcional)<br>Participant '+ (i+1) +': <input type="text" name="part'+i+'"><br><br>';
						}
					  
					};

					
					document.getElementById('parti').innerHTML = txt;
				}
				
				
				
			</script>

			<h3> Incripció <?php echo $_GET['torneig']?></h3>
			<form name="inscrip" action="../process/p_inscrip.php" method="POST" accept-charset="utf-8">
				Nom equip: <input type="text" name="nom_equip" required="required" autofocus="autofocus"> <br>
				Categoria: 
					<select name="cat" onChange="actualitzaInfo()">
						<?php
							for ($i=0; $i < sizeof($cats); $i++) { 
						?>
								<option value="<?php echo $cats[$i]['nom']?>"> <?php echo $cats[$i]['nom']?> </option>
						<?php
							}
						?>
					</select> <br>
				Descripció:
					<div id="desc"> </div> <br>
			
		
				<font size="2" color="red">Recordeu que el dni és el vostre identificador, és imprescindible que sigui correcte </font>
				<br>
				Dni participants: <br><br>
				<div id="parti"> </div> <br>
				
				<input type="hidden" name="torneig" value="<?php echo $_GET['torneig']?>">
				
				<input type="submit" value="Enviar">
			<form>
		</div>
			
	<script>
		actualitzaInfo();
	</script>

    </body>
</html>